package com.sociodox.utils
{
	import flash.utils.*;
	
	public class Base64
	{
		private static const _encodeChars:Vector.<int> = InitEncodeChar();
		private static const _decodeChars:Vector.<int> = InitDecodeChar();
		
		public function Base64()
		{
		}
		
		public static function InitEncodeChar() : Vector.<int>
		{
			var arr:* = new Vector.<int>(64, true);
			var str:String = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
			var i:int = 0;
			while (i < 64)
			{
				
				arr[i] = str.charCodeAt(i);
				i++;
			}
			
			return arr;
		}
		
		public static function InitDecodeChar() : Vector.<int>
		{
			var arr:Vector.<int> = new Vector.<int>(256);
			arr[0] = -1;
			arr[1] = -1;
			arr[2] = -1;
			arr[3] = -1;
			arr[4] = -1;
			arr[5] = -1;
			arr[6] = -1;
			arr[7] = -1;
			arr[8] = -1;
			arr[9] = -1;
			arr[10] = -1;
			arr[11] = -1;
			arr[12] = -1;
			arr[13] = -1;
			arr[14] = -1;
			arr[15] = -1;
			arr[16] = -1;
			arr[17] = -1;
			arr[18] = -1;
			arr[19] = -1;
			arr[20] = -1;
			arr[21] = -1;
			arr[22] = -1;
			arr[23] = -1;
			arr[24] = -1;
			arr[25] = -1;
			arr[26] = -1;
			arr[27] = -1;
			arr[28] = -1;
			arr[29] = -1;
			arr[30] = -1;
			arr[31] = -1;
			arr[32] = -1;
			arr[33] = -1;
			arr[34] = -1;
			arr[35] = -1;
			arr[36] = -1;
			arr[37] = -1;
			arr[38] = -1;
			arr[39] = -1;
			arr[40] = -1;
			arr[41] = -1;
			arr[42] = -1;
			arr[43] = 62;
			arr[44] = -1;
			arr[45] = -1;
			arr[46] = -1;
			arr[47] = 63;
			arr[48] = 52;
			arr[49] = 53;
			arr[50] = 54;
			arr[51] = 55;
			arr[52] = 56;
			arr[53] = 57;
			arr[54] = 58;
			arr[55] = 59;
			arr[56] = 60;
			arr[57] = 61;
			arr[58] = -1;
			arr[59] = -1;
			arr[60] = -1;
			arr[61] = -1;
			arr[62] = -1;
			arr[63] = -1;
			arr[64] = -1;
			arr[65] = 0;
			arr[66] = 1;
			arr[67] = 2;
			arr[68] = 3;
			arr[69] = 4;
			arr[70] = 5;
			arr[71] = 6;
			arr[72] = 7;
			arr[73] = 8;
			arr[74] = 9;
			arr[75] = 10;
			arr[76] = 11;
			arr[77] = 12;
			arr[78] = 13;
			arr[79] = 14;
			arr[80] = 15;
			arr[81] = 16;
			arr[82] = 17;
			arr[83] = 18;
			arr[84] = 19;
			arr[85] = 20;
			arr[86] = 21;
			arr[87] = 22;
			arr[88] = 23;
			arr[89] = 24;
			arr[90] = 25;
			arr[91] = -1;
			arr[92] = -1;
			arr[93] = -1;
			arr[94] = -1;
			arr[95] = -1;
			arr[96] = -1;
			arr[97] = 26;
			arr[98] = 27;
			arr[99] = 28;
			arr[100] = 29;
			arr[101] = 30;
			arr[102] = 31;
			arr[103] = 32;
			arr[104] = 33;
			arr[105] = 34;
			arr[106] = 35;
			arr[107] = 36;
			arr[108] = 37;
			arr[109] = 38;
			arr[110] = 39;
			arr[111] = 40;
			arr[112] = 41;
			arr[113] = 42;
			arr[114] = 43;
			arr[115] = 44;
			arr[116] = 45;
			arr[117] = 46;
			arr[118] = 47;
			arr[119] = 48;
			arr[120] = 49;
			arr[121] = 50;
			arr[122] = 51;
			arr[123] = -1;
			arr[124] = -1;
			arr[125] = -1;
			arr[126] = -1;
			arr[127] = -1;
			arr[128] = -1;
			arr[129] = -1;
			arr[130] = -1;
			arr[131] = -1;
			arr[132] = -1;
			arr[133] = -1;
			arr[134] = -1;
			arr[135] = -1;
			arr[136] = -1;
			arr[137] = -1;
			arr[138] = -1;
			arr[139] = -1;
			arr[140] = -1;
			arr[141] = -1;
			arr[142] = -1;
			arr[143] = -1;
			arr[144] = -1;
			arr[145] = -1;
			arr[146] = -1;
			arr[147] = -1;
			arr[148] = -1;
			arr[149] = -1;
			arr[150] = -1;
			arr[151] = -1;
			arr[152] = -1;
			arr[153] = -1;
			arr[154] = -1;
			arr[155] = -1;
			arr[156] = -1;
			arr[157] = -1;
			arr[158] = -1;
			arr[159] = -1;
			arr[160] = -1;
			arr[161] = -1;
			arr[162] = -1;
			arr[163] = -1;
			arr[164] = -1;
			arr[165] = -1;
			arr[166] = -1;
			arr[167] = -1;
			arr[168] = -1;
			arr[169] = -1;
			arr[170] = -1;
			arr[171] = -1;
			arr[172] = -1;
			arr[173] = -1;
			arr[174] = -1;
			arr[175] = -1;
			arr[176] = -1;
			arr[177] = -1;
			arr[178] = -1;
			arr[179] = -1;
			arr[180] = -1;
			arr[181] = -1;
			arr[182] = -1;
			arr[183] = -1;
			arr[184] = -1;
			arr[185] = -1;
			arr[186] = -1;
			arr[187] = -1;
			arr[188] = -1;
			arr[189] = -1;
			arr[190] = -1;
			arr[191] = -1;
			arr[192] = -1;
			arr[193] = -1;
			arr[194] = -1;
			arr[195] = -1;
			arr[196] = -1;
			arr[197] = -1;
			arr[198] = -1;
			arr[199] = -1;
			arr[200] = -1;
			arr[201] = -1;
			arr[202] = -1;
			arr[203] = -1;
			arr[204] = -1;
			arr[205] = -1;
			arr[206] = -1;
			arr[207] = -1;
			arr[208] = -1;
			arr[209] = -1;
			arr[210] = -1;
			arr[211] = -1;
			arr[212] = -1;
			arr[213] = -1;
			arr[214] = -1;
			arr[215] = -1;
			arr[216] = -1;
			arr[217] = -1;
			arr[218] = -1;
			arr[219] = -1;
			arr[220] = -1;
			arr[221] = -1;
			arr[222] = -1;
			arr[223] = -1;
			arr[224] = -1;
			arr[225] = -1;
			arr[226] = -1;
			arr[227] = -1;
			arr[228] = -1;
			arr[229] = -1;
			arr[230] = -1;
			arr[231] = -1;
			arr[232] = -1;
			arr[233] = -1;
			arr[234] = -1;
			arr[235] = -1;
			arr[236] = -1;
			arr[237] = -1;
			arr[238] = -1;
			arr[239] = -1;
			arr[240] = -1;
			arr[241] = -1;
			arr[242] = -1;
			arr[243] = -1;
			arr[244] = -1;
			arr[245] = -1;
			arr[246] = -1;
			arr[247] = -1;
			arr[248] = -1;
			arr[249] = -1;
			arr[250] = -1;
			arr[251] = -1;
			arr[252] = -1;
			arr[253] = -1;
			arr[254] = -1;
			arr[255] = -1;
			
			return arr;
		}
		
		public static function encode(param1:ByteArray) : String
		{
			var _loc_6:uint = 0;
			var _loc_2:* = new ByteArray();
			_loc_2.length = (2 + param1.length - (param1.length + 2) % 3) * 4 / 3;
			var _loc_3:int = 0;
			var _loc_4:* = param1.length % 3;
			var _loc_5:* = param1.length - _loc_4;
			var _loc_7:int = 0;
			while (_loc_3 < _loc_5)
			{
				
				_loc_6 = param1[int(_loc_3++)] << 16 | param1[int(_loc_3++)] << 8 | param1[int(_loc_3++)];
				_loc_2[int(_loc_7++)] = _encodeChars[int(_loc_6 >>> 18)];
				_loc_2[int(_loc_7++)] = _encodeChars[int(_loc_6 >>> 12 & 63)];
				_loc_2[int(_loc_7++)] = _encodeChars[int(_loc_6 >>> 6 & 63)];
				_loc_2[int(_loc_7++)] = _encodeChars[int(_loc_6 & 63)];
			}
			if (_loc_4 == 1)
			{
				_loc_6 = param1[int(_loc_3)];
				_loc_2[int(_loc_7++)] = _encodeChars[int(_loc_6 >>> 2)];
				_loc_2[int(_loc_7++)] = _encodeChars[int((_loc_6 & 3) << 4)];
				_loc_2[int(_loc_7++)] = 61;
				_loc_2[int(_loc_7++)] = 61;
			}
			else if (_loc_4 == 2)
			{
				_loc_6 = param1[int(_loc_3++)] << 8 | param1[int(_loc_3)];
				_loc_2[int(_loc_7++)] = _encodeChars[int(_loc_6 >>> 10)];
				_loc_2[int(_loc_7++)] = _encodeChars[int(_loc_6 >>> 4 & 63)];
				_loc_2[int(_loc_7++)] = _encodeChars[int((_loc_6 & 15) << 2)];
				_loc_2[int(_loc_7++)] = 61;
			}
			return _loc_2.readUTFBytes(_loc_2.length);
		}
	}
}